<?php

namespace App\Models;


use App\Service\BangCache;

class SiteConfig extends Base
{

    // 系统配置
    protected $table = 'site_config';

    // 缓存字段，用于搜索绑定的字段
    protected $cacheFields = ['id', 'field'];

    // 获取配置信息
    public function getConfig()
    {
        $lists  = $this->get()->toArray();
        $config = array_column($lists, 'value', 'field');

        return $config;
    }


    // 获取配置信息 缓存
    public function getConfigCache($bangData)
    {
        if($bangData === false){
            return $this->getConfig();
        }

        // 获取缓存
        $args = [];
        //$args      = compact('where');
        $bang  = new BangCache();
        $cName = $bang->makeCacheKey($this->table, 'list', $args);   // 所有参数执行 key
        $lists = $bang->getCache($cName);
        if(!empty($lists)){
            return array_column($lists, 'value', 'field');
        }

        $lists  = $this->get()->toArray();
        $config = array_column($lists, 'value', 'field');
        // 保存为缓存
        // 保存缓存
        $bang->addCache($cName, $lists);
        // 保存为绑定
        $this->addBang($bangData, $cName, 'list');


        return $config;
    }


    // 定义验证操作
    public function rules()
    {

        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            'field' => 'required',
            'value' => 'required',
        ];


        $msg = [
            'id'       => '自编号',
            'field'    => '字段',
            'name'     => '中文名',
            'value'    => '中文值',
            'describe' => '描述',
            'ctime'    => '时间',
            'mtime'    => '时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add];
    }
}
